<?php
namespace Plu\qdh\Controllers\Api;

use Plu\qdh\Controllers\Api\ControllerBase;
use Plu\qdh\Services\ProductService;
use Plu\qdh\Services\ThirdService;
use Plu\qdh\Services\AdminService;
use Plu\qdh\Services\QrService;
use Plu\qdh\Services\FileService;

/**
 * @RoutePrefix("/admin")
 */
class AdminController extends ControllerBase {

    /**
	 * @Get('/adminLogin')
	 */
    public function adminLoginAction() {
        $userName = $this->request->getQuery('username');
        $pswd     = $this->request->getQuery('pswd');
        $adminLogin = $this->passport->adminLogin($userName, $pswd);
        if ($adminLogin) {
            return $this->release([
                'code' => 1
            ]);
        }
        return $this->release([
            'code' => 0
        ]);
    }

    /**
	 * @Get('/allProduct')
     * 获取第三方分销系统所有产品
	 */
    public function allProductAction() {
        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }
        $keyWord = $this->request->getQuery('keyword');
        $page    = $this->request->getQuery('page','int',1);
        $thirdService = new ThirdService;
        if ($keyWord) {
            $thirdService->setKeyWord($keyWord);
        }
        list($count, $request) = $thirdService->getThridAllProduction($page);
        return $this->release([
            'count' => $count,
            'items' => $request
        ]);
    }

    /**
	 * @Get('/getProductInfo')
     * 平台系统产品详情
	 */
    public function getProductInfoAction() {
        $productNo   = $this->request->getQuery('productNo');
        $service     = new ProductService;
        $productInfo = $service->getProductDetail($productNo);
        return $this->release($productInfo);
    }


    /**
	 * @Get('/saveProductInfo')
     * 更改票信息
	 */
    public function saveProductInfoAction() {
        $productNo   = $this->request->getQuery('productNo');
        $productData = $this->request->getQuery('productData');
        $service = new ProductService;
        $productData  = json_decode($productData, true);
        $cacheProduct = $service->cacheProduct($productNo, $productData);
        return $this->release([
            'code' => 1
        ]);
    }

    /**
	 * @Get('/productDetail')
     * 分销系统产品详情
	 */
    public function getProductDetailAction() {
        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }
        $productNo = $this->request->getQuery('productNo');
        $thirdService = new ThirdService;
        $thirdService->setProductNo($productNo);
        $detail = $thirdService->getProductDetail();
        if (!empty($detail)) {
            return $this->release([
                'code' => 1,
                'item' => $detail
            ]);
        }
        return $this->release([
            'code' => 0,
            'item' => []
        ]);
    }

    /**
	 * @Get('/getAllProject')
     * 获取所有项目
	 */
    public function getAllProjectAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $platformId = $this->request->getQuery('platformId');

        $service = new ProductService;
        if ($platformId) {
            $service->setPlatformId($platformId);
        }
        $items   = $service->getAllProject();
        return $this->release($items);
    }

    /**
	 * @Get('/getProjectList')
     * 获取项目列表
	 */
    public function getProjectListAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $service = new ProductService;
        $items   = $service->getProjectList();
        return $this->release($items);
    }

    /**
	 * @Post('/addProject')
     * 添加项目
	 */
    public function addProjectAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }
        $postData    = $this->request->getJsonRawBody(true);
        $projectId   = $postData['projectId'];
        $projectData = $postData['projectData'];
        $service     = new ProductService;
        $addProject  = $service->addProject($projectId, $projectData);
        if ($addProject) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]); 
    }

    /**
	 * @Get('/delProject')
     * 删除项目
	 */
    public function delProjectAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $projectId   = $this->request->getQuery('projectId');
        $projectData = $this->request->getQuery('projectData');
        $service     = new ProductService;
        $del         = $service->delProject($projectId, $productData);
        if ($del) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]); 
    }

    /**
	 * @Get('/onSale')
     * 上架
	 */
    public function onSaleAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $projectId   = $this->request->getQuery('projectId');
        $platformId  = $this->request->getQuery('platformId');
        $service     = new ProductService;
        $onSale      = $service->setPlatformProject($platformId, $projectId);
        if ($onSale) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]);
    }

    /**
	 * @Get('/offSale')
     * 下架
	 */
    public function offSaleAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $projectId   = $this->request->getQuery('projectId');
        $platformId  = $this->request->getQuery('platformId');
        $service     = new ProductService;
        $onSale      = $service->delPlatformProject($platformId, $projectId);
        if ($onSale) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]);
    }

    /**
	 * @Get('/addPlatform')
     * 添加栏目
	 */
    public function addPlatformAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $platformName = $this->request->getQuery('platformName');
        $platformId   = $this->request->getQuery('platformId');
        $service      = new ProductService;
        $addPlatform  = $service->addPlatForm($platformId, $platformName);
        if ($addPlatform) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]);        
    }

    /**
	 * @Get('/getAllPlatform')
     * 获取所有栏目
	 */
    public function getAllPlatformAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $service = new ProductService;
        $items   = $service->getAllPlatform();
        return $this->release($items);
    }

    /**
	 * @Get('/delPlatform')
     * 删除栏目
	 */
    public function delPlatformAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $platformId = $this->request->getQuery('platformId');
        $service    = new ProductService;
        $delete     = $service->delPlatForm($platformId);
        if ($delete) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]);
    }

    /**
	 * @Get('/addProduct')
     * 添加商品
	 */
    public function addProductAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $productData = $this->request->getQuery('productData');
        $productData = json_decode($productData, true);
        $service     = new ProductService;
        $service->addProduct($productData);
        return $this->release(['code' => 1]);
    }

    /**
	 * @Get('/delProductRelation')
     * 添加商品
	 */
    public function delProductRelationAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $projectId = $this->request->getQuery('projectId');
        $productNo = $this->request->getQuery('productNo');
        $service   = new ProductService;
        $service->delProductRelation($projectId, $productNo);
        return $this->release(['code' => 1]);
    }

    /**
	 * @Get('/getOrders')
     * 后台商品列表
	 */
    public function getOrdersAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        $page = $this->request->getQuery('page', 'int', 1);

        $fillter = [
            'status'    => $this->request->getQuery('status'),
            'linkPhone' => $this->request->getQuery('linkPhone'),
            'orderId'   => $this->request->getQuery('orderId')
        ];

        $fillter = array_filter($fillter, function ($value) {
			return $value!==NULL;
        });

        $limit  = 30;
        $offset = ($page - 1)*$limit; 

        $service = new AdminService;
        list($count, $items) = $service->getOrders($limit, $offset, $fillter);

        return $this->release([
            'count' => $count,
            'items' => $items
        ]);

    }

    /**
	 * @Post('/uploadImg')
     * 图片上传
	 */
    public function uploadImgAction() {

        $isAdminLogin = $this->passport->isAdminLogin();
        if (!$isAdminLogin) {
            return $this->needLogin();
        }

        if($this->request->hasFiles() == true){
            $service = new FileService;
            foreach($this->request->getUploadedFiles() as $file){
                $fileSize  = $file->getSize();
                if ($fileSize > \AliCloud::PIC_MAX_SIZE) {
                    return $this->release(['code' => 0, 'msg'=>'请上传小于4M的图片']);
                } 
                $type = $file->getType();
                if ($type != 'image/jpeg' && $type != 'image/png') {
                    return $this->release(['code' => 0, 'msg'=>'请上传png或jpeg格式的图片']);
                }
                $fileName  =  time().$file->getName();
                $localPath = APP_CACHE_PATH.time().$fileName;
                $filePath  = $file->moveTo($localPath);
                $fileUrl   = $service->uploadFile($localPath, $fileName);
            }
            return $this->release(['code' => 1, 'path'=>$fileUrl]);
        }
        return $this->release(['code' => 0, 'msg'=>'上传失败']);
    }


    /**
	 * @Get('/makeQr')
     * 创建分销二维码
	 */
    public function makeQrAction() {
        $shopName = $this->request->getQuery('shopName');
        $service  = new QrService;
        $createQr = $service->createQrDistribution($shopName);
        return $this->release($createQr);
    }


    /**
	 * @Get('/updateQr')
     * 更新二维码名称
	 */
    public function updateQrAction() {
        $changeId   = $this->request->getQuery('changeId');
        $changeName = $this->request->getQuery('changeName');
        $service = new QrService;
        $changename = $service->changeQrName($changeId, $changeName);
        if ($changename) {
            return $this->release(['code' => 1]);
        }
        return $this->release(['code' => 0]);
    }

    /**
	 * @Get('/getQrList')
     * 分销商列表
	 */
    public function getQrListAction() {
        $service = new QrService;
        $page    = $this->request->getQuery('page', 'int', 1);
        $limit   = 20;
        $offset  = ($page - 1) * $limit;
        list($count, $items) = $service->getQrList($limit, $offset);
        return $this->release([
            'items' => $items,
            'count' => $count
        ]);
    }

    /**
	 * @Get('/applyRefind')
     * 通过退款审核
	 */
    public function applyRefindAction() {
        $service = new AdminService;
        $orderId = $this->request->getQuery('orderId');
        $apply   = $service->applyRefindOrder($orderId);
        return $this->release($apply);

    }

    /**
	 * @Get('/rejectRefind')
     * 通过退款审核
	 */
    public function rejectRefindAction() {
        $service = new AdminService;
        $orderId = $this->request->getQuery('orderId');
        $reject  = $service->rejectCancelOrder($orderId);
        return $this->release($reject);
    }





}